<template>
  <div>

    <el-card class="box-card">
      <div>
        <el-form label-width="80px"
                 class="form-control">

          <el-form-item label="岗位编码">
            <el-input v-model="queryData.postCode"
                      size="small"></el-input>
          </el-form-item>
          <el-form-item label="岗位名称">
            <el-input v-model="queryData.postName"
                      size="small"></el-input>
          </el-form-item>
          <el-form-item class="sercheItem"
                        label="状态">
            <!-- <el-input></el-input> -->

            <el-row type="flex"
                    :gutter="4"
                    align="middle">
              <el-col :span="1.5">

              </el-col>
              <el-col :span="10">

                <el-select v-model="queryData.status"
                           size="small"
                           placeholder="请选择">
                  <el-option label="正常"
                             :value="0">
                  </el-option>

                  <el-option label="停用"
                             :value="1">
                  </el-option>

                </el-select>
              </el-col>
              <el-col :span="1.5">
                <el-button size="small"
                           type="primary"
                           icon="el-icon-search"
                           @click="search">搜索</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button icon='el-icon-refresh'
                           size="small"
                           @click="reset">重置</el-button>
              </el-col>
            </el-row>
          </el-form-item>
        </el-form>
      </div>
      <div class="btnList">
        <el-row :gutter="8"
                type="flex"
                align="middle"
                class="btn_nav">
          <el-col :span="1.5">
            <el-button size="mini"
                       type="primary"
                       icon="el-icon-plus"
                       plain
                       @click="addXqq">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button size="mini"
                       type="success"
                       icon="el-icon-edit"
                       plain
                       @click="updatadata"
                       :disabled='checkItem.length!=1'>修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button size="mini"
                       type="danger"
                       icon="el-icon-delete"
                       plain
                       :disabled='checkItem.length==0'
                       @click="delMore">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button size="mini"
                       type="warning"
                       icon="el-icon-download"
                       @click="exportdata"
                       plain>导出</el-button>
          </el-col>
        </el-row>

      </div>
      <el-table :data="tableData"
                v-loading="loading"
                @selection-change="handleSelectionChange"
                style="width: 100%"
                :header-cell-style="{background:'#f8f8f9','text-align':'center'}"
                :cell-style="{'text-align':'center'}">
        <el-table-column type="selection"
                         width="120">
        </el-table-column>
        <el-table-column prop="postId"
                         label="岗位编号">
        </el-table-column>
        <el-table-column prop="postCode"
                         label="岗位编码">
        </el-table-column>
        <el-table-column prop="postName"
                         label="岗位名称">

        </el-table-column>

        <el-table-column prop="postSort"
                         label="岗位排序">
        </el-table-column>
        <el-table-column prop="status"
                         label="状态">
          <template slot-scope="scope">
            <span>{{scope.row.status==0?'正常':'停用'}}</span>

          </template>
        </el-table-column>
        <el-table-column prop="createTime"
                         label="创建时间">
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="data">
            <el-button icon="el-icon-edit"
                       type="text"
                       @click="updateHandler(data.row.postId)">修改</el-button>
            <el-button icon="el-icon-delete"
                       type="text"
                       @click="deleteHandler(data.row.postId)">删除</el-button>
          </template>

        </el-table-column>
      </el-table>
      <div class="page"
           v-show="total!=0">
        <el-pagination background
                       @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page="queryData.pageNum"
                       :page-sizes="[3, 5, 7, 9]"
                       :page-size="queryData.pageSize"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="total">
        </el-pagination>
      </div>
    </el-card>
    <div>
      <el-dialog title=""
                 @closed="closed"
                 :visible.sync="dialogVisible"
                 width="30%">
        <el-form :model="formData"
                 :rules="rules"
                 ref="ruleForm"
                 label-width="80px">
          <el-form-item label="岗位名称"
                        prop="postName">
            <el-input v-model="formData.postName"
                      placeholder="请输入名字名称"></el-input>
          </el-form-item>

          <el-form-item label="岗位编码"
                        prop="postCode">
            <el-input v-model="formData.postCode"
                      placeholder="请输入岗位编码"></el-input>
          </el-form-item>

          <el-form-item prop="postSort"
                        label="岗位顺序">
            <el-input-number v-model="formData.postSort"
                             controls-position="right"
                             :min="0"
                             :max="10000000"></el-input-number>
          </el-form-item>
          <el-form-item label="岗位状态">
            <template>
              <el-radio v-model="formData.status"
                        :label="0">正常</el-radio>
              <el-radio v-model="formData.status"
                        :label="1">停用</el-radio>
            </template>

          </el-form-item>
          <el-form-item label="岗位备注">
            <el-input type="textarea"
                      v-model="formData.remark"
                      placeholder="岗位备注"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer"
              class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary"
                     @click="addXqqType">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogVisible: false,
      checkItem: [],
      queryData: {
        pageNum: 1,
        pageSize: 5,
        postCode: null,
        postName: null,
        status: null
      },
      loading: false,
      tableData: [],
      total: 0,
      formData: {
        createBy: null,
        createTime: null,
        flag: false,
        params: {},
        postCode: null,
        postId: null,
        postName: null,
        postSort: null,
        remark: null,
        searchValue: null,
        status: 0,
        updateBy: null,
        updateTime: null
      },
      rules: {
        postName: [{ required: true, message: '请输入岗位名称', trigger: 'blur' }],
        postCode: [{ required: true, message: '请输入编码名称', trigger: 'blur' }],
        postSort: [{ required: true, message: '请输入岗位顺序', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.getDate()
  },
  methods: {
    updatadata() {
      this.tip = false
      this.updateHandler(this.checkItem[0].postId)
    },
    async updateHandler(id) {
      let { data } = await this.$axios.get(`system/post/${id}`)
      if (data.code != 200) return this.$message.error(data.msg)
      this.formData = data.data
      this.dialogVisible = true
    },
    async getDate() {
      this.loading = true
      let { data } = await this.$axios.get('system/post/list', { params: this.queryData })
      console.log(data)
      this.tableData = data.rows
      this.total = data.total
      this.loading = false
    },
    handleSizeChange(val) {
      this.queryData.pageSize = val
      this.getDate()
    },
    handleCurrentChange(val) {
      this.queryData.pageNum = val
      this.getDate()
    },
    search() {
      this.getDate()
    },
    reset() {
      this.queryData = {
        pageNum: 1,
        pageSize: 5,
        postCode: null,
        postName: null,
        status: null
      }
      this.getDate()
    },
    async exportdata() {
      this.$confirm('是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.getExport()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    async getExport() {
      let { data } = await this.$axios.get('system/post/export', { params: this.getParams })
      if (data.code != 200) {
        this.$message.error('失败')
      } else {
        window.location.href = 'https://jumeng.xzjumeng.com/prod-api/common/download?fileName=' + encodeURI(data.msg) + '&delete=' + true
      }
    },
    closed() {
      this.formData = {
        createBy: null,
        createTime: null,
        flag: false,
        params: {},
        postCode: null,
        postId: null,
        postName: null,
        postSort: null,
        remark: null,
        searchValue: null,
        status: 0,
        updateBy: null,
        updateTime: null
      }
    },
    async addXqqType() {
      let data = null
      if (!this.formData.postId) {
        data = await this.$axios.post('system/post', this.formData)
      } else {
        data = await this.$axios.put('system/post', this.formData)
      }

      if (data.data.code != 200) return this.$message.error(data.data.msg)
      this.$message({
        message: data.data.msg,
        type: 'success'
      })
      this.dialogVisible = false
      this.getDate()
    },

    addXqq() {
      this.tip = true
      this.dialogVisible = true
    },

    delMore() {
      let ids = this.checkItem.map((el) => el.postId)
      this.deleteHandler(ids)
    },
    deleteHandler(id) {
      this.$confirm(`是否确认删除编号为${id.toString()}的数据项?`, '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          let data = await this.$axios.delete(`system/post/${id.toString()}`)
          console.log(data)
          if (data.data.code != 200) this.$message.error(data.data.msg)
          this.$message({
            type: 'success',
            message: data.data.msg
          })
          this.getDate()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    handleSelectionChange(val) {
      this.checkItem = val
    }
  }
}
</script>

<style lang="scss" scoped>
.form-control {
  display: flex;
  justify-content: space-around;
}
.page {
  margin-top: 15px;
  display: flex;
  justify-content: flex-end;
}
</style>